package com.huy.jdbc;

import com.huy.bean.User;

import java.sql.*;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

public class JdbcMain {

//    static {
//        Class.forName("");
//    }

    static String url = "jdbc:mariadb://localhost:3306/mybatis_example";
    static String userName = "root";
    static String password = "huyang";

    public static void main(String[] args) throws SQLException {
        Connection conn = null;
        PreparedStatement stmt=null;
        ResultSet rs = null;
        List<User> list=new ArrayList<>();
        try {
            String sql = "select * from t_user";
            conn = DriverManager.getConnection(url, userName, password);
            stmt = conn.prepareStatement(sql);
            rs = stmt.executeQuery();
            while(rs.next()){
                User user=new User();
                String id=rs.getString("id");
                String userName=rs.getString("user_name");
                LocalDateTime createTime=rs.getTimestamp("create_time").toLocalDateTime();
                user.setId(id);
                user.setUserName(userName);
                user.setCreateTime(createTime);
                list.add(user);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            if(rs!=null){
                rs.close();
            }
            if(stmt!=null){
                stmt.close();
            }
            if(conn!=null){
                conn.close();
            }
        }
        if(list.isEmpty()){
            System.out.println("======");
        }
        for (User user:list){
            System.out.println("id:"+user.getId()+" crateTime:"+user.getCreateTime());
        }

    }
}
